package net.hn.hnms.biz.plan.group.controller;

import cn.hutool.core.lang.tree.Tree;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.hn.hnms.biz.api.plan.RemotePlanService;
import net.hn.hnms.biz.plan.group.service.GroupService;
import net.hn.hnms.biz.plan.request.*;
import net.hn.hnms.biz.plan.service.IStatisticService;
import net.hn.hnms.biz.plan.vo.*;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@Api(value = "集团端模块", tags = {"集团端"})
@RestController
@RequestMapping("/group")
@EnableScheduling
public class GroupController {


    @Autowired
    private GroupService groupService;

    @Resource
    private RemotePlanService remotePlanService;


    @Autowired
    private IStatisticService statisticService;


    @ApiOperation("获取销售数据")
    @GetMapping("/getSaleStatisticByDate")
    public R getSaleStatsticData(@Param("date") String date) throws Exception {

        if(StringUtils.isBlank(date)){
            // 创建Calendar对象
            Calendar calendar = Calendar.getInstance();
            // 将当前时间减去一天
            calendar.add(Calendar.DATE, -1);
            // 创建一个日期格式化对象
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            // 格式化新的时间
            date = format.format(calendar.getTime());
        }
        log.info("执行定时任务-获取销售数据,请求数据时间："+date);
        int i = groupService.insertSale(date);
        return R.ok(i);
    }


    @ApiOperation("获取产量数据")
    @GetMapping("/getProductionDataByDate")
    public R getProductionData(@Param("date") String date) throws Exception {

        if(StringUtils.isBlank(date)){
            // 创建Calendar对象
            Calendar calendar = Calendar.getInstance();
            // 将当前时间减去一天
            calendar.add(Calendar.DATE, -1);
            // 创建一个日期格式化对象
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            // 格式化新的时间
            date = format.format(calendar.getTime());
        }

        log.info("获取产量数据-执行定时任务,请求数据时间："+date);


        int i = groupService.insertOutput(date);

        return R.ok(i);
    }


    @ApiOperation("获取进尺数据")
    @GetMapping("/getFootageData")
    public R getFootageData(@Param("date") String date) throws Exception {

        if(StringUtils.isBlank(date)){
            // 创建Calendar对象
            Calendar calendar = Calendar.getInstance();
            // 将当前时间减去一天
            calendar.add(Calendar.DATE, -1);
            // 创建一个日期格式化对象
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            // 格式化新的时间
            date = format.format(calendar.getTime());
        }

        log.info("获取进尺数据-执行定时任务,请求数据时间："+date);


        int i = groupService.insertFootage(date);

        return R.ok(i);
    }


    @ApiOperation("手动获取本年龙软产量和销量数据的全部数据")
    @GetMapping("/getProductionAndSaleData")
    public R getProductionAndSaleData() throws Exception {
        Calendar calendar = Calendar.getInstance();
        calendar.set(2024, 0, 1); // 2014年1月1日
        String startDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        String endDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        List<String> twoDaysDayDes = getTwoDaysDayDes(startDate, endDate);

        for (int i = 0; i < twoDaysDayDes.size(); i++) {
            String s = twoDaysDayDes.get(i);
            log.info(s + "日期------------------------日期");
            int w = groupService.insertOutput(s);
        }
        for (int i = 0; i < twoDaysDayDes.size(); i++) {
            String s = twoDaysDayDes.get(i);
            log.info(s + "日期------------------------日期");
            int w = groupService.insertSale(s);
        }
        for (int i = 0; i < twoDaysDayDes.size(); i++) {
            String s = twoDaysDayDes.get(i);
            log.info(s + "日期------------------------日期");
            int w = groupService.insertFootage(s);
        }
        return R.ok();
    }

    @ApiOperation("获取环保监测月度考核排名")
    @GetMapping("/getMonthlyAssessmentRanking")
    public R getMonthlyAssessmentRanking() throws Exception {

        log.info("执行定时任务-获取环保监测月度考核排名");
        int i = groupService.getMonthlyAssessmentRanking();
        return R.ok(i);
    }

    @ApiOperation("获取环保监测传输有效率")
    @GetMapping("/getEfficientTransmission")
    public R getEfficientTransmission() throws Exception {

        log.info("执行定时任务-获取环保监测传输有效率");
        int i = groupService.getEfficientTransmission();
        return R.ok(i);
    }

    @ApiOperation("获取环保List数据Json")
    @GetMapping("/getEnvironmentProtection")
//    @AutoSetMineCode
    public R<List<EnvironmentProtectionVo>> getEnvironmentProtection() throws Exception {
        return R.ok(statisticService.getEnvironmentProtection());
    }

    @ApiOperation("查询获取环保List数据")
    @GetMapping("/selectEnvironmentProtection")
//    @AutoSetMineCode
    public R<List<EnvironmentProtectionVo>> selectEnvironmentProtection() throws Exception {
        return R.ok(statisticService.selectEnvironmentProtection());
    }
    @ApiOperation("查询监测数据总量")
    @GetMapping("/getMonitoringData")
//    @AutoSetMineCode
    public R<List<EnvironmentProtectionVo>> getMonitoringData() throws Exception {
        return R.ok(statisticService.getMonitoringData());
    }

    @ApiOperation("根据月份获取传输有效率")
    @GetMapping("/getEfficientTransmissionByMonth")
//    @AutoSetMineCode
    public R<Double> getEfficientTransmissionByMonth() throws Exception {
        return R.ok(statisticService.getEfficientTransmissionByMonth());
    }


    @ApiOperation("经营综合概览-近6个月销量集团端")
    @GetMapping("/SaleBarChartCount")
//    @AutoSetMineCode
    public R<List<ExecutionSalesVo>> SaleBarChartCount(SalesPlanReq req) {
        return R.ok(statisticService.GroupSaleBarChartCount(req));
    }

    @ApiOperation("经营综合概览-销量饼图集团端")
    @GetMapping("/executionSaleCount")
//    @AutoSetMineCode
    public R<List<ExecutionSalesVo>> executionSaleCount(SalesPlanReq req) {
        return R.ok(statisticService.groupExecutionSaleCount(req));
    }


    @ApiOperation("经营综合概览-销售计划集团端")
    @GetMapping("/groupSaleCompanyPlan")
//    @AutoSetMineCode
    public R<List<MonthValue>> groupSaleCompanyPlan(Integer month, String companyCode) {
        return R.ok(statisticService.groupSaleCompanyPlan(month,companyCode));
    }

    private static List<String> getTwoDaysDayDes(String startDate, String endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> dateList = new ArrayList<>();
        try {
            Date dateOne = sdf.parse(startDate);
            Date dateTwo = sdf.parse(endDate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateOne);
            dateList.add(startDate);
            while (dateTwo.after(calendar.getTime())) {
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                dateList.add(sdf.format(calendar.getTime()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateList;
    }

    @ApiOperation("生产成本管理合计集团端")
    @GetMapping("/selectExecutionCostTotalByCompanyCodeOrName")
    public R<List<ExecutionCostVO>> selectExecutionCostTotalByCompanyCodeOrName(ExecutionCostReq planMining ) {
        return R.ok(groupService.selectExecutionCostTotalByCompanyCodeOrName(planMining));
    }

    @ApiOperation("洗选管理合计集团端")
    @GetMapping("/selectExecutionWashingTotalByCompanyCodeOrName")
    public R<List<ExecutionWashingVO>> selectExecutionWashingTotalByCompanyCodeOrName(ExecutionWashingReq planMining ) {
        return R.ok(groupService.selectExecutionWashingTotalByCompanyCodeOrName(planMining));
    }


    @ApiOperation("获取掘进计划列表集团端")
    @GetMapping("/selectDrivageTotalByCompanyCodeOrName")
    public R<List<PlanDrivageVO>> selectDrivageTotalByCompanyCodeOrName(PlanDrivageReq planMining ) {
        return R.ok(groupService.selectDrivageTotalByCompanyCodeOrName(planMining));
    }


    @ApiOperation("获取洗选计划列表集团端")
    @GetMapping("/selectWashingTotalByCompanyCodeOrName")
    public R<List<PlanWashingVO>> selectWashingTotalByCompanyCodeOrName(PlanWashingReq planMining ) {
        return R.ok(groupService.selectWashingTotalByCompanyCodeOrName(planMining));
    }

    @ApiOperation("其他生产计划合计集团端")
    @GetMapping("/selectOtherTotalByCompanyCodeOrName")
    public R<List<PlanOtherVO>> selectOtherTotalByCompanyCodeOrName(PlanOtherReq planMining ) {
        return R.ok(groupService.selectOtherTotalByCompanyCodeOrName(planMining));
    }

    @ApiOperation("回采工作面合计集团端")
    @GetMapping("/selectDimMiningFaceTotalByCompanyCodeOrName")
    public R<List<DimMiningFaceVO>> selectDimMiningFaceTotalByCompanyCodeOrName(DimMiningFaceReq planMining ) {
        return R.ok(groupService.selectDimMiningFaceTotalByCompanyCodeOrName(planMining));
    }


    @ApiOperation("掘进工作面合计集团端")
    @GetMapping("/selectDimDrivageFaceTotalByCompanyCodeOrName")
    public R<List<DimDrivageFaceVO>> selectDimDrivageFaceTotalByCompanyCodeOrName(DimDrivageFaceReq planMining ) {
        return R.ok(groupService.selectDimDrivageFaceTotalByCompanyCodeOrName(planMining));
    }

    @ApiOperation("产量计划合计集团端")
    @GetMapping("/selectPlanOutputTotalByCompanyCodeOrName")
    public R<List<PlanOutputVO>> selectPlanOutputTotalByCompanyCodeOrName(PlanOutputReq planMining ) {
        return R.ok(groupService.selectPlanOutputTotalByCompanyCodeOrName(planMining));
    }

    @ApiOperation("进尺产量合计集团端")
    @GetMapping("/selectFootageOutputTotalByCompanyCodeOrName")
    public R<List<ExecutionFootageOutputVO>> selectFootageOutputTotalByCompanyCodeOrName(ExecutionFootageOutputReq planMining ) {
        return R.ok(groupService.selectFootageOutputTotalByCompanyCodeOrName(planMining));
    }

    @ApiOperation("其他生产管理合计集团端")
    @GetMapping("/selectExecutionOtherTotalByCompanyCodeOrName")
    public R<List<ExecutionOtherVO>> selectExecutionOtherTotalByCompanyCodeOrName(ExecutionOtherReq planMining ) {
        return R.ok(groupService.selectExecutionOtherTotalByCompanyCodeOrName(planMining));
    }
    @ApiOperation("获取回采计划列表集团端")
    @GetMapping("/selectMiningTotalByCompanyCodeOrName")
    public R<List<PlanMiningVO>> selectMiningTotalByCompanyCodeOrName(PlanMiningReq planMining) {
        return R.ok(groupService.selectMiningTotalByCompanyCodeOrName(planMining));
    }


    @ApiOperation("回采工作面合计集团端")
    @PostMapping("/selectDimMiningFaceName")
    public R< List<Tree<Integer>>> selectDimMiningFaceName(DimMiningFaceReq planMining) {
        return R.ok(groupService.selectDimMiningFaceName(planMining));
    }


    @ApiOperation("驾驶舱-销售情况-年月日销量")
    @GetMapping("/sale/getGroupSale")
    public R getGroupYield() {
        return R.ok(groupService.getGroupYield(null));
    }


    @ApiOperation("驾驶舱-销售情况-计划与实际对比")
    @GetMapping("/sale/planVersusPractice")
    /**
     * DateType=0是日，1是月，2是年
     * DataType=0是煤炭产业，1是煤业公司，2是矿
     */
    public R planVersusPractice(SaleAnalyseReq req) {
        return R.ok(groupService.planVersusPractice(req));
    }


    @ApiOperation("驾驶舱-销售情况-销售变化趋势")
    @GetMapping("/sale/variationTrend")
    public R variationTrend(SaleAnalyseReq req) {
        return R.ok(groupService.variationTrend(req));
    }


    @ApiOperation("驾驶舱-销售情况-库存")
    @GetMapping("/sale/inventory")
    public R inventory() {
        return R.ok(groupService.inventory());
    }


}
